package com.atmilan.quartz.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atmilan.quartz.mapper.StoreProductAttrMapper;
import com.atmilan.quartz.model.dto.crmeb.StoreProductAttrSearchRequest;
import com.atmilan.quartz.model.entity.crmeb.StoreProductAttr;
import com.atmilan.quartz.model.result.crmeb.PageParamRequest;
import com.atmilan.quartz.service.IStoreProductAttrService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;

/**
* @author Mr.Zhang
* @description StoreProductAttrServiceImpl 接口实现
* @date 2020-05-27
*/
@DS("crmeb")
@Service
public class StoreProductAttrServiceImpl extends ServiceImpl<StoreProductAttrMapper, StoreProductAttr> implements IStoreProductAttrService {
    @Autowired
    StoreProductAttrMapper storeProductAttrMapper;

    /**
    * 列表
    * @param request 请求参数
    * @param pageParamRequest 分页类参数
    * @author Mr.Zhang
    * @since 2020-05-27
    * @return List<StoreProductAttr>
    */
    @Override
    public List<StoreProductAttr> getList(StoreProductAttrSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        //带 StoreProductAttr 类的多条件查询
        StoreProductAttr model = new StoreProductAttr();
        BeanUtils.copyProperties(request, model);

        return list(Wrappers.<StoreProductAttr>lambdaQuery().setEntity(model));
    }

    @Override
    public Boolean batchSave(List<StoreProductAttr> storeProductAttrs) {
        if (null == storeProductAttrs) {
            return false;
        }
        for (StoreProductAttr attr : storeProductAttrs) {
            if (save(attr)) {
                log.warn("新增产品属性失败");
            }
        }

        return true;
    }

    @Override
    public Boolean batchUpdate(List<StoreProductAttr> storeProductAttrs) {
        if (null == storeProductAttrs) {
            return false;
        }
        for (StoreProductAttr storeProductAttr : storeProductAttrs) {
            UpdateWrapper<StoreProductAttr> lwq = new UpdateWrapper<>();
            lwq.eq("product_id", storeProductAttr.getProductId());
            lwq.eq("attr_name", storeProductAttr.getAttrName());
            lwq.eq("attr_values", storeProductAttr.getAttrValues());
            lwq.eq("type", storeProductAttr.getType());
            if (update(storeProductAttr, lwq))
                log.warn("更新产品属性失败");
        }
        
        return true;
    }

    @Override
    public List<StoreProductAttr> getByProductId(int productId) {
        LambdaQueryWrapper<StoreProductAttr> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StoreProductAttr::getProductId, productId);
        List<StoreProductAttr> storeProductAttrs = list(lambdaQueryWrapper);
        return storeProductAttrs;
    }

    @Override
    public void removeByProductId(Integer productId) {
        LambdaQueryWrapper<StoreProductAttr> lambdaQW = new LambdaQueryWrapper<>();
        lambdaQW.eq(StoreProductAttr::getProductId, productId);
        remove(lambdaQW);
    }

    @Override
    public void deleteAll() {
        storeProductAttrMapper.deleteAll();
    }
    
}
